//
//  XMLRegionalShow.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/5/25.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XMLRegionalShow.h"
#import "XXTool.h"

@implementation XMLRegionalShow


- (instancetype)init
{
    self=[super init];
    if(self)
    {
        self.isDownload=NO;
        self.isOrigin = NO;
        self.imgUrl = @"";
        self.process=0;
        self.imgTitle = @"";
    }
    return self;
}

+(void)requestRegionalUseDataWithCompletion:(void (^)(BOOL, NSMutableArray<XMLRegionalShow *> *))block
{
    [DKHTTPRequest sendDataWithShortUrl:@"areaphotoshowing"
                                  Param:[XXTool formatStringWithLanguage:@""]
                             ImageArray:nil
                                TimeOut:10
                             Completion:^BOOL(NSMutableDictionary *serverDic, DKHTTPResponseType bufferType) {
                                 if ([[serverDic objectForKey:@"code"] intValue]==200)
                                 {
                                     NSMutableArray <XMLRegionalShow*> *list = [NSMutableArray array];
                                     NSArray *tempArray = [serverDic objectForKey:@"list"];
                                     for (NSDictionary * temp in tempArray)
                                     {
                                         [list addObject:[XMLRegionalShow modelWithDictionary:temp]];
                                     }
                                     if (block)
                                     {
                                         block(YES,list);
                                     }
                                     
                                 }
                                 else
                                 {
                                     if (block)
                                     {
                                         block(NO,nil);
                                     }
                                 }
                                 return YES;
                             } IsBuffering:YES
                      IsNeedReplaceWrap:NO
                        IsUseUpLoadHead:NO];
}

/**
 *  开始下载图片
 */
- (void)beginDownload
{
    self.isDownload=YES;
    self.isOrigin = NO;
    [[DKImageSqlite shareImageSqlite] loadImage:self.imgUrl
                                    placeHolder:nil
                                         target:nil
                                        process:^(long long totalByte, long long receiveByte, BOOL isFinish) {
                                            if(totalByte!=-1)
                                            {
                                                self.process=receiveByte*1.0/totalByte;
                                            }
                                            if([self.delegate respondsToSelector:@selector(XMLRegionalShow:didReceiveSize:totalSize:image:isFinish:)])
                                            {
                                                [self.delegate XMLRegionalShow:self
                                                                didReceiveSize:receiveByte
                                                                     totalSize:totalByte
                                                                         image:nil
                                                                      isFinish:NO];
                                            }
                                        }
                                 enableDownload:YES
                                     completion:^(UIImage *imageData) {
                                         self.isDownload=NO;
                                         if(imageData!=nil)
                                         {
                                             self.isOrigin=YES;
                                         }
                                         if([self.delegate respondsToSelector:@selector(XMLRegionalShow:didReceiveSize:totalSize:image:isFinish:)])
                                         {
                                             [self.delegate XMLRegionalShow:self
                                                             didReceiveSize:0
                                                                  totalSize:0
                                                                      image:imageData
                                                                   isFinish:YES];
                                         }
                                     }];
}

@end
